package springboot.tools.service;

import com.google.common.collect.Lists;
import org.apache.poi.ss.usermodel.*;
import springboot.tools.model.Column;
import springboot.tools.utils.ExcelUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author liumeng27
 * @date 2020/7/28
 * description
 */
public class Create {
    /**
     * 创建Excel的过程
     * 1 创建表头
     */
    public void createExcel(){
        Workbook workbook = ExcelUtil.createWriteWorkbook();
        Sheet sheetSummary1 = workbook.createSheet("示例表单");
        //获取原始的表头信息
        List<Object> originColumn=buildExcelTitle("合并单元格");
        //处理表头 如合并单元格
        List<Column> columnListSummary1 = ExcelUtil.createTitleColumn(originColumn,0,0);
        //处理前三行合并格式
        processThirdLineStyle(workbook, columnListSummary1);
        //创建表头
        CellStyle titleStyle = ExcelUtil.getCellStyle(workbook, (short) 10, true, true);
        ExcelUtil.createHead(columnListSummary1, sheetSummary1, titleStyle);


        int rowLine = 4;
        //创建数据
        Row row = sheetSummary1.createRow(rowLine); //行数 在sheet的第四行数据
        //序号
        row.createCell(1).setCellValue("数据1");
        //表项
        row.createCell(2).setCellValue("数据2");

        //设置cell格式
        CellStyle bodyStyle = ExcelUtil.getCellStyle(workbook, (short) 10, true, false);
        Cell cell = row.getCell(2);
        cell.setCellStyle(bodyStyle);
        ExcelUtil.autoSizeColumn(sheetSummary1, cell, 2);

    }
    private List<Object> buildExcelTitle(String target){
        //表头前部分
        List<Object> titleList = Lists.newArrayList(
                Lists.newArrayList("sheetName"),
                Lists.newArrayList("所属税期：" + "yearMo"),
                Lists.newArrayList("单位："));

        String state="合并单元格";
        if(!target.equals(state)){
            List<Object> head = new ArrayList<>();
            head = Lists.newArrayList(Arrays.asList("序号", "公司名称", "累计亏损(期初)", "累计亏损(期末)", "变动情况(+增加/-减少)"));
            titleList.add(head);
        }
        else if(target.equals(state)){
            titleList.add(Arrays.asList(new HashMap<String, List<String>>() {{
                put("", Lists.newArrayList("编号"));
            }},new HashMap<String, List<String>>() {{
                put("基础信息", Lists.newArrayList("公司段", "公司实体", "公司名称", "是否已注销", "所属bg", "税务管理主R", "所得税税率"));
            }},new HashMap<String, List<String>>() {{
                put("期间", Lists.newArrayList("期间"));
            }},new HashMap<String, List<String>>() {{
                put("财务情况", Lists.newArrayList("收入总额", "毛利", "毛利率", "利润总额", "税前利润率"));
            }},new HashMap<String, List<String>>() {{
                put("税务资产", Lists.newArrayList("累计亏损(期初)", "累计亏损(期末)", "累计亏损占比(期初)"));
            }},new HashMap<String, List<String>>() {{
                put("实缴税金", Lists.newArrayList("所属期口径税金", "入库期口径税金", "税金占比(入库)"));
            }},new HashMap<String, List<String>>() {{
                put("关联交易利润", Lists.newArrayList("第三方利润", "关联交易收入", "关联交易支出", "关联交易利润"));
            }},new HashMap<String, List<String>>() {{
                put("税金影响因素", Lists.newArrayList("关联交易对实缴税金的影响", "税率对实缴税金的影响"));
            }}));
        }
        return titleList;
    }

    /**
     * 处理前三行
     */
    private void processThirdLineStyle(Workbook workbook, List<Column> columnList) {
        CellStyle noBorderTitleStyle = ExcelUtil.getCellStyle(workbook, (short) 10, false, true);
        //单独处理前三行的表头格式
        for (int i = 0; i < columnList.size(); i++) {
            if (i < 3) {
                Column column = columnList.get(i);
                column.setcLen(3);
                column.setCellRangeBorder(false);
                noBorderTitleStyle.setAlignment(HorizontalAlignment.LEFT);
                column.setCellStyle(noBorderTitleStyle);
            }
        }
    }
}
